import { Global } from "../../../Game/info/Global";
import MusicManager from "../../../MusicManager";
import Scene from "../../../Scene";
import { TButton, TLayout } from "../../../SceneControl";
import StartGameScene from "./StartGameScene";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChooseScene extends Scene {

    @property(cc.Node)
    point: cc.Node = null;
    targetNode: cc.Node = null;

    protected onEnable(): void {
        super.onEnable();
        this.initScene();
    }

    reset(targetNode: cc.Node, index: number) {
        this.targetNode = targetNode;
        if (index <= 2) {
            this.point.y = 175;
            this.point.scaleY = 1;
        } else {
            this.point.y = -175;
            this.point.scaleY = -1;
        }

        switch (index) {
            case 0:
                this.node.position = cc.v3(-185, -120)
                break;
            case 1:
                this.node.position = cc.v3(0, -120)
                break;
            case 2:
                this.node.position = cc.v3(185, -120)
                break;
            case 3:
                this.node.position = cc.v3(-185, 145)
                break;
            case 4:
                this.node.position = cc.v3(0, 145)
                break;
            case 5:
                this.node.position = cc.v3(185, 145)
                break;
        }
        index *= 3;

        let iconFrame = this.node.parent.parent.getComponent(StartGameScene).propIcon;
        let slotSprite = targetNode.getChildByName("icon").getComponent(cc.Sprite);
        this.node.getChildByName("bg").children.forEach(item => {
            let propInfo = Global.PropInfo[index];
            item.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = iconFrame[index];
            item.getChildByName("name").getComponent(cc.Label).string = propInfo.名字;
            item.getChildByName("infor").getComponent(cc.Label).string = propInfo.说明;
            let btn = item.getChildByName("yellowBtn").getComponent(cc.Button);
            btn.clickEvents[0].customEventData = index.toString();
            if (Global.gameInfo.propCount[index] > 0) {
                btn.getComponent(cc.Sprite).setMaterial(0,cc.Material.getBuiltinMaterial('2d-sprite'));
            } else {
                btn.getComponent(cc.Sprite).setMaterial(0,cc.Material.getBuiltinMaterial('2d-gray-sprite'));
            }
            if (slotSprite.spriteFrame === iconFrame[index]) {
                btn.node.getChildByName("选这个").getComponent(cc.Label).string = "已选择";
            } else {
                btn.node.getChildByName("选这个").getComponent(cc.Label).string = "选这个";
            }

            cc.find("numbg/New Label", item).getComponent(cc.Label).string = Global.gameInfo.propCount[index].toString();
            index++;
        })
    }

    public initScene() {
        let layout = new TLayout();
        layout.buttonList = [this.node.getComponentsInChildren(cc.Button).map(but => new TButton(but.node))]
        this.tscene.addLayout(layout);
        this.tscene.onBack = () => {
            this.node.active = false;
        }
        this.tscene.joinController();
    }

    choose(target, date) {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let index = parseInt(date);
        if (Global.gameInfo.propCount[index] == 0) {
            return this.node.parent.parent.getComponent(StartGameScene).showTip();
        }

        this.node.parent.parent.getComponent(StartGameScene).onChoseChange(index);
        this.node.active = false;
    }
}
